﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jy123.BLL.Star
{
    public class Star
    {
        /// <summary>
        /// 购买球星教练
        /// </summary>
        /// <param name="user">购买球星教练的用户</param>
        /// <param name="club">购买球星教练的俱乐部</param>
        /// <param name="star">购买的球星教练</param>
        /// <returns>是否成功</returns>
        public bool BuyStar(int userId, int clubId, int starId)
        {
            try
            {
                User_Main BLLUser = new User_Main();
                Club_Main BLLClub = new Club_Main();
                Public_Star BLLStar = new Public_Star();
                Club_StarRecord BLLRecord = new Club_StarRecord();
                Model.User_Main user = BLLUser.GetModel(userId);
                Model.Club_Main club = BLLClub.GetModel(clubId);
                Model.Public_Star star = BLLStar.GetModel(starId);
                decimal money = 0;
                if (star.Price.HasValue)
                {
                    money = star.Price.Value;
                }

                Model.Club_StarRecord record = new Jy123.Model.Club_StarRecord();
                record.FClub = club.ID;
                record.FStar = star.ID;
                record.SType = star.FSType.Value;
                BLLRecord.Add(record);
                user.Cash = Convert.ToInt32(user.Cash.Value - money);
                if (club.Assets.HasValue)
                {
                    club.Assets = club.Assets.Value + money;
                }
                else
                {
                    club.Assets = money;
                }
                BLLUser.Update(user);
                BLLClub.Update(club);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
